'''
Created on May 9, 2011

@author: Chris
'''
import Feature
import Suggestion
import FeatureSuggestions
import SentenceGrammar
import FeatureHandler
import Document
import DocumentTokenizer
import en

class SentenceLength(object):
    '''
    classdocs
    '''


    def __init__(self, featureHandler, feature):
        '''
        Constructor
        '''
        self.featureHandler = featureHandler
        self.feature = feature
        self.sentenceLengthSuggestions = []
        self.featureSuggestions = []
        self.grammar = SentenceGrammar.SentenceGrammar()
        #self.doc = doc
        
        #avgLength = feature.value
        
       
    def suggest(self, avgSentenceLength, rating):
        shortSentenceLen = 0
        longSentenceLen = 4000
        suggestionsMade = 0
        replacement = ''
        #for section in self.doc.sections:
        for section in self.featureHandler.documentSections:
            for sentence in section.sentences:
                if (sentence.text.find(';') > -1):
                    
                    halves = sentence.text.split(';')
                    sentenceOne = halves[0] + "."
                    sentenceTwo = halves[1].strip()
                    sentenceTwo = sentenceTwo.capitalize()
                    replacement = sentenceOne + ". " + sentenceTwo
                    #make suggestion
                    suggestionsMade += 1
                elif (sentence.text.find("and") > -1):
                    sentenceText = sentence.text
                    count = sentenceText.count("and")
                    lastIndex = 0
                    for i in range(0,count):
                        andIndex = sentenceText.find("and", lastIndex)
                        sentenceOne = sentenceText[:andIndex].strip()
                        sentenceTwo = sentenceText[andIndex + 4:].strip()

                        if ((self.isIndependentClause(sentenceOne) == True) and (self.isIndependentClause(sentenceTwo[:-1]) == True)):
                            #make suggestion
                            replacement = self.joinClauses(sentenceOne, sentenceTwo)
                            #don't do this, just for testing
                            #return replacement
                        lastIndex = andIndex + 4

                        featureSuggestion = FeatureSuggestions.FeatureSuggestions(self.feature)
                        self.sentenceLengthSuggestions = Suggestion.Suggestion('Sentence change', self.featureHandler.document, "".join(sentence.text), replacement, '#FF0000')
                        featureSuggestion.addSuggestions(self.sentenceLengthSuggestions)
                        self.featureSuggestions.append(featureSuggestion)
        
    def isIndependentClause(self, text):
        """
        Returns true if the text is and independent cluase, false otherwise
        """
        #ignore quotations
        chunks = en.sentence.chunk(text)
        independent = self.grammar.checkChunks(chunks)
        return independent
    
    def joinClauses(self, clause1, clause2):
        clause1 = clause1 + "."
        clause2 = clause2.strip()
        clause2 = clause2.capitalize() + "."
        joined = clause1 + " " + clause2
        return joined
    
    def getFeatureSuggestions(self):
        return self.featureSuggestions
        

if __name__ == '__main__':
    sentenceText = "I don't like to lie and, the dog is black."
    token = DocumentTokenizer.DocumentTokenizer()
    doc = token.tokenizeDocument(sentenceText)
    SentenceLength = SentenceLength(False, False, doc)
    
    #print SentenceLength.suggest(3, 3)
    #a = en.sentence.chunk("It is now the second week of high school")
    #print a
    pass